home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / prolog_1.zip / PROLOG.DOC < prev    next >
Text File  |  1987-12-08  |  99KB  |  3,433 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.                            AUTOMATA DESIGN ASSOCIATES
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.                      A.D.A PROLOG Documentation Version 1.95C
  33.                   for the Educational and Public Domain Versions
  34.  
  35.                                   June 21, 1987
  36.  
  37.               Copyright Robert Morein and Automata Design Associates
  38.  
  39.                                  1570 Arran Way
  40.                                Dresher, Pa. 19025
  41.  
  42.                                  (215)-646-4894
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                             
  71.              
  72.                                    News
  73.  
  74.         A.D.A.  offers  the first Prolog with a Cyclic Structure  Unifier 
  75.         for the IBM PC.
  76.  
  77.              If  you  have CGA graphics capability,  be sure to run  "The 
  78.         Knight's Tour",  by Tim Elliot,  to be found in GAMES.ARC. It's a 
  79.         graphic  representation  of a classic puzzle:  how  to  make  the 
  80.         "knight"  piece  of chess,  which has a rather peculiar  movement 
  81.         rule, visit all the squares of a chess board.
  82.  
  83.              Javier  Salazar  has contributed  a  wang  algorithm,  which 
  84.         determines the correctness of predicate calculus formulae,  and a 
  85.         logic tutor.
  86.  
  87.              Jim   Adams   of   Bendix  Aerospace   has   contributed   a 
  88.         skolemizer  program  (look for the file "LOGIC.ARC".  This  nifty 
  89.         thing takes statements written in the formalism of the  predicate 
  90.         calculus  and  converts them to prolog Horn clauses.  You  should 
  91.         refer to chapter 10 of Clocksin and Mellish for explanation.
  92.  
  93.  
  94.              Take a look at the chess endgame program in "games."
  95.  
  96.              Simon   Blackwell's   "PIE"  Truth  Maintenance  System   is 
  97.         presented in revised, debugged, and enlarged form. This system is 
  98.         found  in  the  directory  "expert"  and  augments  the  strictly 
  99.         deductive  capabilities  of raw Prolog with additional  forms  of 
  100.         reasoning.  PIE  has a syntax that resembles colloquial  English. 
  101.         Wait till you see the backwards quote marks!
  102.  
  103.              The predicates "batch" and "nobatch" are introduced to allow 
  104.         execution  of batch files without confusing messages and  prompts 
  105.         appearing on the screen. I've put one in Simon's "KOPS" file.
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.                                         1
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.                                 Copyright Notice
  137.  
  138.              The  public domain PD PROLOG system has been contributed  to 
  139.         the  public domain for unrestricted use with one  exception:  the 
  140.         object  code  may not be  disassembled  or  modified.  Electronic 
  141.         bulletin  boards  and SIG groups are urged to aid in giving  this 
  142.         software the widest possible distribution.
  143.  
  144.              This documentation may be reproduced freely,  but it may not 
  145.         be  included in any other documentation without the permission of 
  146.         the author.
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.                                         2
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.                                   Introduction
  203.  
  204.         We  hope  that you'll get some fun out of this  PROLOG.  It  will 
  205.         afford  you exposure to THE fifth generation language at the cost 
  206.         only  of  some  intellectual  effort.  The  motive  is  perfectly 
  207.         explicable:  We  want you to think of Automata Design  Associates 
  208.         for  fifth  generation  software.  It also gives us a  nice  warm 
  209.         feeling.
  210.  
  211.         The  minimum  memory  requirement is 200 k of  transient  program 
  212.         area,  plus  whatever  space is needed to execute  programs  from 
  213.         within PROLOG.  DOS or MSDOS 2.0 are required.  The program  does 
  214.         not  require  IBM PC compatibility to run,  although  the  screen 
  215.         access routines do require compatibility.
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.                                         3
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.                      Products by Automata Design Associates
  269.  
  270.              Automata  Design  Associates  specializes  in  software  for 
  271.         artificial  intelligence  and  robotic  applications.   A  PROLOG 
  272.         language  system is available in various configurations.  A  LISP  
  273.         interpreter will be introduced in March of 1985.
  274.          
  275.  
  276.  
  277.         1. LISP systems
  278.  
  279.              PD LISP
  280.  
  281.         PD  LISP is a public domain Common LISP subset.  It comes with  a 
  282.         150 page instruction manual on disk, and is available from us for 
  283.         $10.00.
  284.  
  285.              .UNX LISP
  286.  
  287.         UNX LISP, written by Dave Morein, is a Common LISP subset. It has 
  288.         nifty  things like SAVE/RESTORE,  which let you stop a job in the 
  289.         middle,  save  it,  and start it up again,  and  tree  structured 
  290.         lexical scoping. It is available from us for $69.95.
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.                                         4
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.         I.PROLOG Systems
  335.  
  336.              There  are  six versions of PROLOG available  from  Automata 
  337.         Design Associates.  The systems run under the MSDOS or PCDOS. All 
  338.         of them access all available memory up to 1 megabyte.
  339.  
  340.         .Public Domain PROLOG
  341.  
  342.         This  serves to further the general awareness of the public about 
  343.         PROLOG.  It  also is an excellent adjunct to anyone learning  the 
  344.         language.  Most  of  the core PROLOG described  by  Clocksin  and 
  345.         Mellish  in  the book Programming In  PROLOG  is  implemented.  A 
  346.         complete  IBM PC video screen support library is included in this 
  347.         and  all other A.D.A.  prologs.  Trace predicates are  not.  This 
  348.         version is available from us for $10.00 postage paid.
  349.  
  350.  
  351.         .Educational PROLOG
  352.  
  353.         At  extremely modest cost this affords an educational institution 
  354.         or  individual  a  PROLOG  system  which  provides  the   maximum 
  355.         available  programming  area  available  within  the  8086  small 
  356.         programming model.  Tracing,  a debugging aid,  allows monitoring 
  357.         a program as it runs.  User settable spy points selectively allow 
  358.         this.  Exhaustive  tracing  is also  available.  I/O  redirection 
  359.         gives some file ability.
  360.  
  361.              An  "exec"  function allows the execution of  a  program  or 
  362.         editor  from  within  PROLOG,  thus  encouraging  an  interactive 
  363.         environment.
  364.  
  365.              An  "interrupt"   menu is added,  permitting the control  of 
  366.         tracing, toggling the printer, and screen printing.
  367.  
  368.              Definite clause grammar support is now included.
  369.              
  370.              The cost of Educational PROLOG is $29.95.
  371.  
  372.  
  373.  
  374.         .FS PROLOG
  375.  
  376.              A  small  increment  in price adds full random  access  file 
  377.         capability. Character and structure I/O are allowed.
  378.  
  379.              The  "asserta and "assertz" predicates are expanded and work 
  380.         with a clause indexing ability.  One can assert clauses  anywhere 
  381.         in the database under precise pattern matching control. 
  382.  
  383.              A tree structured lexical scoping system and floating  point 
  384.         arithmetic are other enhancements.
  385.  
  386.              The cost of FSM PROLOG is $49.95
  387.  
  388.  
  389.  
  390.  
  391.                                         5
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.         .VMI PROLOG -- Virtual Memory (Replaces type VMS)
  402.  
  403.  
  404.              At reasonable cost the addition of virtual memory gives  an 
  405.         expansion of capabilities of an order of magnitude. 
  406.  
  407.              The  database on disk is treated transparently.  No  special 
  408.         provisions  need  be  made  by the  user.  Virtual  and  resident 
  409.         databases  may be mixed.  A unique updating algorithim  preserves 
  410.         the format of the database as typed by the user while making only 
  411.         those changes necessary to make it equivalent to the database  in 
  412.         central memory.
  413.  
  414.              The cost of VMI PROLOG is $99.95
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.                                         6
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.         .VML PROLOG Large model Virtual Memory System
  467.  
  468.              A.D.A.  PROLOG is a remarkable fifth generation developement 
  469.         tool  for  the  implementation  of  intelligent  strategies   and 
  470.         optimized  control.  It  is both the kernel for  applications  of 
  471.         virtually  unlimited scope and a sophisticated developement  tool 
  472.         that multiplies the productivity of the programmer many times. 
  473.  
  474.              Conventional  Prolog  is based on the first order  predicate 
  475.         calculus.   VML   emulates  the  second  order   calculus,   with 
  476.         unconstrained   manipulation   of   cyclic,    self   referential 
  477.         structures.  The  cyclic unifier is selectable by a mode  switch, 
  478.         with no loss of compatibility. 
  479.  
  480.              Perhaps  only  one  other Prolog system,  written  by  Alain 
  481.         Colmareur,  permits manipulation of cyclic structures.  Permitted 
  482.         by the second order predicate calculus, but not the first, cyclic 
  483.         structures are self referential, or recursively defined.
  484.  
  485.              A conventional Prolog system typically crashes when a cyclic 
  486.         structure  is  found,  because  such a structure  appears  to  be 
  487.         infinite. But with our Cyclic Unifier, available in types VML and 
  488.         VMA Prolog, cyclic structures can be handled with impunity.
  489.  
  490.              Many  structures in nature are cyclic,  and we suggest  that 
  491.         many applications will appear to the user.  For example, consider 
  492.         the  benzene ring,  which chemists refer to as a cyclic  aromatic 
  493.         hydrocarbon:
  494.                                         
  495.  
  496.                                  H             H        
  497.                                   \          /          
  498.                                     C ===== C           
  499.                                   /           \         
  500.                                  /             \        
  501.                           H --- C                C --- H
  502.                                  \\            //       
  503.                                   \\          //        
  504.                                     C ----- C           
  505.                                   /           \         
  506.                                  H             H        
  507.                                             
  508.  
  509.         This  might  be represented in Prolog by a  cyclic  structure  as 
  510.         follows:
  511.                                             
  512.                                             
  513.                X = [ [[c,h]],[c,h],[[c,h]],[c,h],[[c,h]],[c,h]|X]
  514.  
  515.         It  would  appear that groups and topological properties  can  be 
  516.         succinctly described as cyclic structures.  A simple group can be 
  517.         expressed as follows:
  518.  
  519.                                  X = [a,b,c|X]. 
  520.  
  521.  
  522.  
  523.                                         7
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.         Then  the cyclic nature of the group can be observed  with 
  533.         the goal "member( M, X )", which gives:
  534.  
  535.                       a,b,c,a,b,c,a,b,c,....(ad infinitum)
  536.  
  537.              With  a  cost/performance ratio exceeding that of any  other 
  538.         product  and  a  compatibility  insured  by  compliance  to   the 
  539.         Edinburgh syntax, performance is enhanced by numerous extensions, 
  540.         many of them invisible to the user.
  541.  
  542.              A quick overview of some of the features discloses:
  543.  
  544.              1)  This  system now incorporates a cyclic  structure 
  545.              unifier.  The  only  other Prolog system  capable  of 
  546.              unifying  cyclic structures is PrologII,  written  by 
  547.              the inventor of Prolog, Alain Colmerauer. 
  548.  
  549.              2)  Invisible  compilation  to  a  semantic  network 
  550.              preserves the flexibility of the interpreted mode and 
  551.              the speed of a compiler.
  552.  
  553.              The  programmer can compile and recompile any portion 
  554.              of a module at any time.  The edit/compile/test cycle 
  555.              is short and free of strain. An interface is provided 
  556.              to an editor of choice.
  557.  
  558.  
  559.              3) Floating point arithmetic with a full  complement 
  560.              of  input  and  output  methods,  transcendental  and 
  561.              conversion functions.
  562.  
  563.  
  564.              4)  Virtual  memory.  Module  size  and  number  are 
  565.              unrestricted,   with  a  total  capacity  of  several 
  566.              hundred megabytes.  Resident and virtual modules  may 
  567.              be co-resident. Compilation is incremental. The cache 
  568.              algorithim  is  sophisticated.  Changes made  in  the 
  569.              database can be updated to disk by a single command.
  570.  
  571.  
  572.              5) A powerful exec function and acceptance of  stream 
  573.              input make  integration into  applications practical.
  574.  
  575.  
  576.              6)  Global polymorphic variables  retain  information 
  577.              that  would  otherwise  require  the  "assertion"  of 
  578.              facts.
  579.  
  580.  
  581.              7)  A  quoted  variable class,  borrowed  from  LISP, 
  582.              permits  referencing variables as objects as well  as 
  583.              by value.
  584.  
  585.  
  586.  
  587.  
  588.  
  589.                                         8
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.              8) Multidimensional arrays,  dynamically created  and 
  599.              destroyed,  efficiently  store numeric and nonnumeric 
  600.              structures. Arrays are ideal for representing spatial 
  601.              and ordinal relationships.
  602.              
  603.              9)  Debugging facilities let you see your program run 
  604.              without any additional generation steps.
  605.  
  606.  
  607.              10)  Totally   invisible  and   incremental   garbage 
  608.              collection.   There   is  NEVER  any  wait  for  this 
  609.              function.
  610.  
  611.  
  612.              11)  A  tree  structured,   dynamically  configurable 
  613.              lexical scoping system. The work of many  programmers 
  614.              can  be coupled together in the form of libraries and 
  615.              nested domains. 
  616.  
  617.              Each lexically scoped domain is a hidden space  which 
  618.              communicates  with the parent domain via exports  and 
  619.              imports. Domains can be linked together under program 
  620.              control to achieve any desired configuration.
  621.  
  622.  
  623.              12) The Grammar Rule Notation is an integral feature.
  624.  
  625.  
  626.              13) Keyword redefinition makes porting code easy.
  627.  
  628.  
  629.         The  cost  of this system is $200 for the MSDOS version.
  630.  
  631.  
  632.  
  633.         .VMA PROLOG Large model Virtual Memory System
  634.  
  635.         This  system  has  additional forms of  virtual  memory.  It  was 
  636.         intended  for  deep  reasoning  problems.  Contact  us  for  more 
  637.         information.
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.                                         9
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.                              Prolog Runtime Systems
  665.  
  666.  
  667.              The A.D.A.  Runtime Systems permit distribution of  debugged 
  668.         code in source form without royalties.  A Runtime System provides 
  669.         all  of  the facilities of the corresponding  Development  System 
  670.         except for:
  671.  
  672.              1) Tracing.
  673.  
  674.              2)  The  console  command loop.  
  675.  
  676.         A  Runtime  System is invoked from the DOS command line  with  an 
  677.         argument specifying a stream file that replaces the console.  The 
  678.         stream file is a UNIX* innovation that allows applications to  be 
  679.         connected by "pipes", in a convenient, high level manner.
  680.  
  681.              All  the  other  development  facilities  remain  available, 
  682.         including  multiple modules and virtual memory.  Since update  to 
  683.         disk  is  included,  the ability of the system to  learn  is  not 
  684.         impaired in any way.  Compare this to our competitors, whose hard 
  685.         compiled  code results in fixed goals and inability to change the 
  686.         distribution on a dynamic basis.
  687.  
  688.              Four versions are available.  Each is priced the same as the 
  689.         corresponding development system,  and can be distributed without 
  690.         royalty by the license holder with his applications:
  691.              
  692.              
  693.                   FS  Runtime - $50.00
  694.                                  
  695.                   VMI Runtime - $100.00
  696.  
  697.                   VML Runtime - $200.00
  698.  
  699.                   VMA Runtime - $250.00
  700.  
  701.              The above systems are in stock for immediate delivery.
  702.  
  703.  
  704.              * trademark AT&T
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.                                        10
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.                                  Upgrade Policy
  731.  
  732.              Half the cost of any A.D.A. PROLOG system may be credited to 
  733.         the purchase of a higher level version.      The full cost of VMS 
  734.         prolog  may  be  applied to the purchase of VMI  or  VML  PROLOG. 
  735.         Updates to a particular level product vary from $15.00 to $35.00.
  736.  
  737.  
  738.                               Technical Information
  739.  
  740.              Technical  information  may be obtained at  (215)  -  646- 
  741.         4894. This is available provided that you have read the  entire 
  742.         documentation  supplied with this disk, and at least the  first 
  743.         third of Programming In Prolog. We will politely inquiries from 
  744.         people who just want to see the sample programs run.
  745.  
  746.              Perhaps we can answer the following questions in advance:
  747.  
  748.              There is no support for:  APPLE II, Atari, Commodore, or  
  749.         CPM 80 .  Other machines from these manufactures may be supported 
  750.         in the future.
  751.  
  752.              The  MSDOS  products are available on "5"  IBM  compatible 
  753.         diskettes.
  754.  
  755.  
  756.                               To Place Your Order:
  757.  
  758.              You may place your order at the following number:
  759.  
  760.                         (215)-646-4894   - day and night.
  761.  
  762.  
  763.  
  764.                                      Returns
  765.  
  766.              The  software may be returned within 30 days of purchase for 
  767.         a full refund.  This applies whether or not the software has been 
  768.         used.  We do ask that manuals, disks and packaging be returned in 
  769.         excellent condition.
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.                                        11
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.                                   Installation
  797.  
  798.              You will need an IBM PC compatible machine with a minimum of 
  799.         256  kbytes  of  memory.  ED PROLOG benefits from  all  available 
  800.         memory up to the 1 megabyte INTEL limit.
  801.  
  802.              To  determine the amount of TPA your machine  has,  run  the 
  803.         "chkdsk" program which is supplied with DOS. The last line reads:
  804.  
  805.                                 XXXXXX bytes free
  806.  
  807.         where XXXXXX is a six digit number.
  808.  
  809.         If  this  number  is greater than 200000,  ED  PROLOG  will  have 
  810.         reduced workspace.  If it's over 245000,  the amount of memory is 
  811.         optimal. If this is not the case, there are two possibilities:
  812.  
  813.         1) The machine doesn't have enough memory.
  814.  
  815.         2)  Something  else is removing memory from TPA,  such as  a  co-
  816.         resident  program,  a  ramdisk,  a large dos driver,  or a  large 
  817.         number of file or disk buffers.
  818.  
  819.         If  you're  short of memory,  make sure that no  other  programs, 
  820.         ramdisks,  or drivers besides DOS are running in the machine. You 
  821.         may find it helps to eliminate (by renaming) the config.sys  file 
  822.         when you intend to run ED PROLOG.
  823.  
  824.                       How to run the Demonstration Programs 
  825.                         without Knowing What You're Doing
  826.  
  827.              We strongly advise that you purchase the book Programming in 
  828.         PROLOG by Clocksin and Mellish,  publisher Springer Verlag, 1981. 
  829.         For  the  impatient we give some advice.  Type the  demonstration 
  830.         program you wish to run.  There must be at least one entry  point 
  831.         within  the  program.  
  832.  
  833.         Note:  Please  understand that these are demonstrations programs. 
  834.         Regarding  user  interface,  they are poorly  written.  You  will 
  835.         probably have to read Clocksin and Mellish to appreciate that the 
  836.         following examples of input are not equivalent: "yes." , "yes" .
  837.  
  838.              The program fragment "console" shows how you may improve the 
  839.         console input routines of any of these programs.
  840.              
  841.  
  842.         The Hematology Diagnosis Program - "hemat"
  843.  
  844.              Although  the  logical structure is not as sophisticated  as 
  845.         that of "animals", it is interesting for several reasons:
  846.  
  847.              1)  The  program  evaluates numerical data to  arrive  at  a 
  848.         diagnosis.
  849.  
  850.              2) Although inaccurate, it demonstrates that useful question 
  851.  
  852.  
  853.                                        12
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.         answering systems are not difficult to write in PROLOG.
  863.  
  864.              3)  There  are  some mistakes in  the  program,  which  only 
  865.         slightly impede its usefulness. 
  866.  
  867.              This  program  uses  structure  input.  Terminate  all  your 
  868.         answers with a period, as in "y.<CR>", or "no.<CR>".
  869.         The starting point is "signs.<CR>".  PROLOG will prompt  you 
  870.         for  signs  of  anemia.  The  program attempts  to  diagnose  two 
  871.         varieties of a hemolytic anemia.
  872.              The program could use a good working over by a  hematologist 
  873.         and we would be delighted to collaborate.
  874.  
  875.  
  876.         Prime Number Generator - "sieve"
  877.  
  878.              This  program demonstrates that anything can be programed in 
  879.         PROLOG if one tries hard enough.  Asking the  question   
  880.           "primes( 50, L ).<CR>" causes a list of prime numbers less than 
  881.         50  to be printed out.  "Sieve" is heavily recursive and  quickly 
  882.         exhausts the stack space of the small model interpreters.
  883.  
  884.  
  885.         Grrules
  886.  
  887.              This  is  an  example  of the use  of  the  definite  clause 
  888.         grammer  notation.  PD  PROLOG does  not  have  this  translation 
  889.         facility, but ED PROLOG and  all of our other versions do. It  is 
  890.         possible  to  perform  the  translations  by  hand  if  you  have 
  891.         thoroughly  read  C  & M. Then you would  have  the  pleasure  of 
  892.         asking:
  893.  
  894.                  ?-sentence( X, [every,man,loves,a,woman], [] ).
  895.  
  896.         and  having the meaning elucidated as a statment in the predicate 
  897.         calculus.
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.                                        13
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.                                Special Offer  # 1
  929.  
  930.              For  some  inexplicable reason,  demonstration programs  are 
  931.         hard to come by. We are too busy writing PROLOG fill this gap. We 
  932.         will reward the contribution of "cute" sample programs.  Here are 
  933.         the guidlines as of 9/3/86:
  934.  
  935.         1) We've got enough games, puzzles and geneology programs. 
  936.         2) We need:
  937.              a)  Alternate  forms  of reasoning,  such  as  probabalistic 
  938.         (Bayesian, fuzzy logic) forward chaining, cover sets.
  939.  
  940.              b)  Programs which use the graphics.
  941.  
  942.              c)  
  943.         3) Expert systems.
  944.  
  945.         You  must be certain that your contribution does not infringe  on 
  946.         anyone's copyright.
  947.  
  948.         The reward is the following: A copy of VMI virtual memory PROLOG. 
  949.         There will be a charge of $20.00 to cover our costs. That way, we 
  950.         won't go broke rewarding sample programs.
  951.  
  952.         The  sample program will be published as an intact file  together 
  953.         with whatever comments or advertisments the author may see fit to 
  954.         include, on our distribution disks. 
  955.  
  956.         Exceptional  contributions  may  merit a copy of type  VML  large 
  957.         model virtual memory PROLOG which now incorporates a UNIX1  style 
  958.         tree structured domain system.
  959.  
  960.  
  961.  
  962.                                 Special Offer # 2
  963.  
  964.  
  965.              If  you are a hardware manufacturer and would like a  PROLOG 
  966.         language  for your system,  the solution is simple.  Just send us 
  967.         one  of  your machines!  Provided your system  implements  a  "C" 
  968.         compiler, it will be ported in no time flat.
  969.  
  970.  
  971.         ______
  972.              1. Trademark of AT & T.
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.                                        14
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.                          Writing Programs For ED PROLOG
  995.  
  996.              You do not type in programs at the "?-" prompt.  There is no 
  997.         built-in  editor.  The command "consult( user )" is accepted  but 
  998.         does not cause PROLOG to enter an editing mode.  We feel that the 
  999.         most universally acceptable editing method is for the user to use 
  1000.         a text editor of choice,  which can be invoked from within PROLOG 
  1001.         by the "exec" predicate.
  1002.  
  1003.              Use  Wordstar  or your customary editor to write a  program. 
  1004.         Then  run  PD  PROLOG and use the consult function  to  load  the 
  1005.         program.
  1006.  
  1007.              In  all  cases except PD PROLOG,  you can  run  your  editor 
  1008.         without leaving PROLOG by use of the "exec" predicate.
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.                                        15
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.                              Running the Interpreter
  1061.  
  1062.         COMMANDS: Give commands in lower case.
  1063.  
  1064.         TO RUN:
  1065.              Invoke PROLOG.EXE.  After the "?-" prompt appears,  you  may 
  1066.              give Prolog goals to be executed,  followed by a period, and 
  1067.              a carriage return.
  1068.  
  1069.         TO ENTER A FACT:
  1070.              Don't do it except with the "assert" predicates. This is the 
  1071.              most  frequently misunderstood aspect of A.D.A.  Prolog.  If 
  1072.              you want to enter a bunch of facts,  put them in a file  and 
  1073.              "consult" them using the "consult" predicate.
  1074.  
  1075.         TO ASK A QUESTION:
  1076.              At the prompt, type "<expression>.<CR>", where 
  1077.              <expression>  is  a  question as described by  Clocksin  and 
  1078.              Mellish. Be sure to terminate the question with a period.
  1079.              The question may be up to 500 characters long.
  1080.  
  1081.         TO INPUT A STRUCTURE AT THE KEYBOARD:
  1082.              The structure may be up to 500 characters in length. Be sure 
  1083.              to terminate with a period.
  1084.  
  1085.         TO ASK FOR ANOTHER SOLUTION:
  1086.              If a solution has been provided, the PROLOG interpreter will 
  1087.              ask  "More?  (Y/N):".  Only  if  a "y"  is  typed  will  the 
  1088.              interpreter perform a search.
  1089.  
  1090.         TO ABORT A SEARCH:
  1091.              Simply   type   the  escape  key.   The   interpreter   will      
  1092.              respond  with  "Interrrupted.",  and return to  the  command      
  1093.              prompt.
  1094.  
  1095.         TO LOAD DATABASE:
  1096.              Type  "consult(<filename>).<CR>" The file name must have the 
  1097.              extension  ".PRO".  It  is  not  necessary  to  include  the 
  1098.              extension in the argument of consult. The file name as given 
  1099.              must not be the same as a predicate name in the file or  any 
  1100.              file which will be loaded.
  1101.  
  1102.         TO TRACE:
  1103.              When the system displays the prompt "?-", type "trace.<CR>".  
  1104.              The display will likely move too rapidly for you to read. To 
  1105.              stop the display,  type Control S.  To restart the  display, 
  1106.              type  Control S.  To turn the trace      display  off,  type 
  1107.              "notrace<CR>"  at  the  prompt  "?-".   The  interrupt  menu 
  1108.              contains  additional  options,  such  as sending  all  trace 
  1109.              output to a file, as well as display at the console.
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.                                        16
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126.         TO INTERRUPT A PROGRAM:
  1127.              See  the  section  entitled  "The  Interrupt  Menu"  for   a 
  1128.              description  of the flexible options.  Basically,  one types 
  1129.              ESC  to terminate a program,  while Control V or  Control  I 
  1130.              interrupt a program. 
  1131.  
  1132.         TO RECONSULT A FILE:
  1133.              The   predicate  "recon"  is  identical  to  the   Edinburgh 
  1134.              predicate "reconsult."
  1135.  
  1136.         TO REMOVE A DATABASE:
  1137.              Type "forget(<filename>).<CR>"
  1138.  
  1139.         TO REMOVE ALL ASSERTIONS MADE BY PROGRAMS OR YOU:
  1140.              Type "forget( user ).<CR>"
  1141.  
  1142.         TO EXIT TO THE OPERATING SYSTEM:
  1143.              Type "exitsys.<CR>"
  1144.  
  1145.              The system is totally interactive; any commands the operator 
  1146.         gives  are and must be valid program statements.  Statements must 
  1147.         terminate with a period.      All commands which take a file name 
  1148.         also  accept a path name.  Any name which is not a  valid  PROLOG 
  1149.         atom (refer to C & M) must be enclosed in single quotes. Thus one 
  1150.         could say
  1151.  
  1152.              consult( expert )
  1153.  
  1154.         but one would need single quotes with
  1155.  
  1156.              consult( 'b:\samples\subtype\expert' ).
  1157.  
  1158.  
  1159.         To exit the system, type "exitsys.<CR>"
  1160.  
  1161.         Atoms  may contain MSDOS pathnames if they are enclosed by single 
  1162.         quotes, ie.,  '\b:\samples\animal' .
  1163.  
  1164.         You may consult more than one file at a time.  However, all names 
  1165.         are public and name conflicts must be avoided. The order in which 
  1166.         modules are loaded may,  in cases of poor program design,  affect 
  1167.         the behavior.
  1168.  
  1169.  
  1170.  
  1171.                              Command Line Arguments
  1172.  
  1173.              ED and PD PROLOG accept one command line argument,  which is 
  1174.         the name of a "stream" which replaces the console for input.  The 
  1175.         "stream"  in  MSDOS is a pipe or file which supplies input  until 
  1176.         end-of-file is reached. Control then reverts back to the console. 
  1177.         To avoid noisy parser error messages when end-of-file is reached, 
  1178.         the  last command in the file should be "see( user )." See  Simon 
  1179.         Blackwell's PIE program for an example of this.
  1180.  
  1181.  
  1182.  
  1183.                                        17
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.                                A Reference of Note
  1193.  
  1194.  
  1195.              With  minor  exceptions,  the syntax is a superset  of  that 
  1196.         described  by  Clocksin and Mellish in the  book  Programming  in 
  1197.         Prolog by W.F.  Clocksin and C.S.  Mellish, published by Springer 
  1198.         Verlag in Berlin, Heidelberg, New York, and Tokyo. We shall refer 
  1199.         to this book as "C & M".
  1200.  
  1201.                A  book  review section is included at the end  of  this 
  1202.         documentation.
  1203.  
  1204.  
  1205.              There   are  very  few   syntactical   differences,   mostly 
  1206.         unrecognized and/or minor.
  1207.              When  an operator is declared using the "op" statement,  the 
  1208.         operator must be enclosed in single quotes in the "op"  statement 
  1209.         itself,  if  it would not otherwise be a legal Edinburgh functor. 
  1210.         Subsequently,  however,  the parser will recognize it for what it 
  1211.         is,  except  in  the "unop" statement,  where it  must  again  be 
  1212.         enclosed in single quotes.
  1213.  
  1214.              Variable numbers of functor paramaters are supported.
  1215.  
  1216.              A  goal  may be represented by a  variable,  which  is  less 
  1217.         restrictive  than  the  C  &  M requirement  that  all  goals  be 
  1218.         functors.  The  variable must be instantiated to a  functor  when 
  1219.         that goal is pursued.
  1220.  
  1221.              Rules which appear inside other expressions must be enclosed 
  1222.         in  parenthesis  if  the "," operator is to be  recognized  as  a 
  1223.         logical connective.
  1224.  
  1225.              All  infix  operators described by C & M,  and user  defined 
  1226.         infix,  prefix, and postfix operators with variable associativity 
  1227.         and precedence are supported exactly as in C & M.
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.                                        18
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258.                             Memory Size Configuration
  1259.  
  1260.              Type  ED  can  access the full memory of  the  machine,  but 
  1261.         previously required configuation by A.D.A.  The supplied  program 
  1262.         "prconf.exe"  allows  the user to configure the amount of  memory 
  1263.         used  by the PROLOG system.  The reason why this is important  is 
  1264.         that  it is the vacant memory above the system which is  used  by 
  1265.         the  "exec"  function  to load and execute  other  programs.  The 
  1266.         amount of memory used is configured into the PROLOG.EXE file  and 
  1267.         cannot be changed when PROLOG is actually running.
  1268.  
  1269.              The  configuration  is  carried  out  with  PRCONF.EXE   and 
  1270.         PROLOG.EXE on the same disk. The user runs PRCONF and answers the 
  1271.         sole question
  1272.  
  1273.             Enter the amount of workspace required in decimal Kbytes:
  1274.  
  1275.         PRCONF  then modifies the PROLOG.EXE file and writes the  changes 
  1276.         out  to  disk.  This may be done as many times  as  required  and 
  1277.         experimentation   is  beneficial.   There  are  several   factors 
  1278.         competing for memory and they must be balanced:
  1279.  
  1280.               1)  The  amount of workspace must be sufficient to run  the 
  1281.               prolog programs.  The total amount of memory used by PROLOG 
  1282.               given by the following formula:
  1283.  
  1284.                   size of PROLOG.EXE 
  1285.                  +size of workspace selected
  1286.                  +size of space needed to "exec" other programs, if used
  1287.                  +size of "COMMAND.COM" if "exec" is used
  1288.                 ----------------------------------------
  1289.                  = Total used by Prolog system.
  1290.  
  1291.               "Total"  must be less than the available transient  memory. 
  1292.               To get this,  run the "chkdsk" program,  supplied with DOS, 
  1293.               and read the last line of the printout.
  1294.  
  1295.  
  1296.               2)  There must be sufficient memory to "exec"  the  desired 
  1297.               programs.
  1298.  
  1299.               3)  Drivers  and auxiliary programs installed by  the  user 
  1300.               such   as  "sidekick"  or  "superkey"  take  memory  in   a 
  1301.               preemptive  fashion.   You  may  have  some  difficulty  in 
  1302.               determining  how much free memory there  really  is.  After 
  1303.               PRCONF  has  been  run,  load  PROLOG  and  note  that  the 
  1304.               copyright  notice displays the highest memory location used 
  1305.               by PROLOG. 
  1306.  
  1307.         If the selected amount of workspace is greater than the memory of 
  1308.         the  machine allows,  PROLOG simply acquires all available memory 
  1309.         for  its  own  use.  This has only one bad  effect  - the  "exec" 
  1310.         function won't function.
  1311.  
  1312.  
  1313.  
  1314.  
  1315.                                        19
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324.                          The Built In Predicate Library
  1325.  
  1326.                         Available Operators in PD and ED PROLOG
  1327.  
  1328.  
  1329.         Column 1 gives the function symbol. 
  1330.  
  1331.         Column 2 gives the precedence. The range of precedence is 1 to 255.
  1332.         A zero in the precedence column indicates the symbol is parsed as 
  1333.         a functor, and precedence is meaningless in this case.
  1334.  
  1335.         Column 3 gives the associativity. 
  1336.         A zero in the associativity column indicates the symbol is parsed
  1337.         as a functor, and associativity is meaningless in this case.
  1338.  
  1339.         Column 4 indicates which version the function is available in. 
  1340.         Unless otherwise noted, the function is available in all versions.
  1341.         Nonstandard predicates are indicated by "NS".
  1342.  
  1343.  
  1344.         op/pred      precedence    associativity         availability
  1345.  
  1346.         "!"             0             0
  1347.         "|"             0             0
  1348.         "="             40,           XFX                   
  1349.         "=="            40,           XFX                   
  1350.         "\\="           40,           XFX                   
  1351.         "\\=="          40,           XFX       
  1352.         "/"             21,           YFX       
  1353.         "@="            40,           XFX       
  1354.         ">="            40,           XFX       
  1355.         "=<"            40,           XFX       
  1356.         ">"             40,           XFX       
  1357.         "<"             40,           XFX       
  1358.         "-"             31,           YFX        
  1359.         "*"             21,           YFX
  1360.         "+"             31,           YFX
  1361.         "=.."           40,           XFX
  1362.         "-->"           255,          YFY       (not  in  PD PROLOG)
  1363.         "?-"            255,          FY  
  1364.               
  1365.  
  1366.  
  1367.         "arg"           0,            0,                                   
  1368.         "asserta"       0,            0,                                   
  1369.         "assertz"       0,            0,                                   
  1370.         "atom"          0,            0,                                   
  1371.         "atomic"        0,            0,                                   
  1372.         "batch"         0,            0
  1373.         "clause"        0,            0,                                   
  1374.         "clearops"      0,            0,                                   
  1375.         "cls"           0,            0,       NS
  1376.         "concat"        0,            0,                                   
  1377.         "consult"       8,            FX,                                  
  1378.         "crtgmode"      0,            0,       NS
  1379.  
  1380.  
  1381.                                        20
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390.         "crtset"        0,            0,       NS
  1391.         "curset"        0,            0,       NS
  1392.         "curwh"         0,            0,       NS
  1393.         "debugging      0,            0,                                   
  1394.         "dir"           0,            0,                                   
  1395.         "display"       0,            0,                                   
  1396.         "dotcolor"      0,            0,       NS
  1397.         "drawchar"      0,            0,       NS
  1398.         "drawdot"       0,            0,       NS
  1399.         "drawline"      0,            0,       NS
  1400.         "exec"          0,            0,                                   
  1401.         "exitsys"       0,            0,       NS                          
  1402.         "forget"        0,            0,       NS                          
  1403.         "functor"       0,            0,                                   
  1404.         "get0"          8,            FX,                                  
  1405.         "integer"       0,            0,                                   
  1406.         "is"            40,           XFX,                                 
  1407.         "listing"       0,            0,                                   
  1408.         "memleft"       0,            0,      NS
  1409.         "mod"           11,           XFX,                                 
  1410.         "name"          0,            0,                                   
  1411.         "nl"            0,            0,                                   
  1412.         "nodebug"       0,            0,       (not in PD PROLOG)
  1413.         "nonvar"        0,            0,                                   
  1414.         "nospy"         50,           FX,      (not in PD PROLOG)
  1415.         "not"           60            FX                                   
  1416.         "notrace"       0,            0,       (not in PD PROLOG)
  1417.         "op"            0,            0,                                   
  1418.         "popoff"        0,            0,       NS
  1419.         "popoffd"       0,            0,       NS
  1420.         "popon"         0,            0,       NS
  1421.         "popond"        0,            0,       NS
  1422.         "print"         0,            0,                                   
  1423.         "prtscr"        0,            0,       NS
  1424.         "put"           0,            0,                                   
  1425.         "ratom"         0,            0,                                   
  1426.         "read"          0,            0,                                   
  1427.         "recon"         0,            0, (Note: this is "reconsult")
  1428.         "repeat"        0,            0,                                   
  1429.         "retract"       0,            0                                    
  1430.         "rnum"          0,            0,                                   
  1431.         "see"           0,            0,       
  1432.         "seeing"        0,            0,       
  1433.         "seen"          0,            0,       
  1434.         "skip"          0,            0,       (not in PD PROLOG)
  1435.         "spy"           50,           FX,      
  1436.         "tab"           0,            0,                                   
  1437.         "tell"          0,            0,       
  1438.         "telling"       0,            0,       
  1439.         "told"          0,            0,       
  1440.         "trace"         0,            0,       (not in PD PROLOG)
  1441.         "true"          0,            0,                                   
  1442.         "unop"          0,            0,                                   
  1443.         "var"           0,            0,                                   
  1444.         "write"         0,            0,                                   
  1445.  
  1446.  
  1447.                                        21
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456.                         Description of the Modifications
  1457.              
  1458.  
  1459.         I/O Redirection
  1460.  
  1461.         I/O  redirection is a feature described by Clocksin and  Mellish. 
  1462.         The predicates "see",  "seeing",  "seen",  "tell", "telling", and 
  1463.         "told" are used to select the streams used for input and output. 
  1464.  
  1465.              The  predicates  "seen" and "told" require as arguments  the 
  1466.         name of the stream that is to be closed.  This enables the system 
  1467.         to  remember the indices of several streams and switch  back  and 
  1468.         forth between them.
  1469.  
  1470.              The  predicate "batch",  when inserted at the beginning of a 
  1471.         stream file, has the following properties:
  1472.  
  1473.              1)  The normal prompt,  "?-",  and advisory messages do  not 
  1474.              appear at the screen.
  1475.  
  1476.              2)  It is self cancelling if the input stream is  reassigned 
  1477.              to the console.
  1478.  
  1479.              3) It may also be cancelled by the predicate "batch".
  1480.  
  1481.  
  1482.  
  1483.         call( <goal> )
  1484.  
  1485.              The  predicate as defined in C & M is obsolete.  The purpose 
  1486.         was  to permit a goal search where the goal name was  a  variable 
  1487.         instantiated  to  some functor name.  A.D.A.  permits writing  of 
  1488.         goals with such names,  so the mediation of the "call" clause  is 
  1489.         no longer necessary.
  1490.  
  1491.              The  "call"  predicate  may  be  trivially  implemented  for 
  1492.         compatibility with the PROLOG definition
  1493.  
  1494.                                  call( X ) :- X.
  1495.  
  1496.  
  1497.  
  1498.         clause
  1499.  
  1500.              The  function  clause(  X,  Y ) has the  new  optional  form 
  1501.         clause(  X,  Y,  I  ).  If the third variable is written,  it  is 
  1502.         instantiated  to the current address of a clause in  memory.  The 
  1503.         only  use of the result is with succeeding assertfa and  assertfz 
  1504.         statements.
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.  
  1511.  
  1512.  
  1513.                                        22
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522.         debugging
  1523.  
  1524.              "Debugging"  prints a list of the current  spypoints.  After 
  1525.         each name a sequence of numbers may appear, indicating the number 
  1526.         of  arguments that is a condition of the trace.  The  word  "all" 
  1527.         appears  if  the  number of arguments is not a condition  of  the 
  1528.         trace.
  1529.  
  1530.  
  1531.  
  1532.  
  1533.         op( <prec>, <assoc>, <functor> )
  1534.  
  1535.              Defines  the  user  definable  grammar  of  a  functor.  The 
  1536.         definition conforms to that in C & M. We mention here a minor but 
  1537.         important point.  If <functor> is not a valid PROLOG atom it must 
  1538.         be   enclosed  in  single  quotes  when  declared  in  the   "op" 
  1539.         declaration.  It  is  not necessary or legal to do this when  the 
  1540.         functor is actually being used as an operator.  In version 1.6, a 
  1541.         declared  or built-in operator can be used either as an  operator 
  1542.         or as a functor. For example, 
  1543.  
  1544.                                  +(2,3) = 2 + 3.
  1545.  
  1546.         is a true statement.
  1547.  
  1548.              Declared  operators  are annotated in the directory  display 
  1549.         with their precedence and associativity.
  1550.  
  1551.  
  1552.  
  1553.  
  1554.  
  1555.  
  1556.  
  1557.  
  1558.  
  1559.  
  1560.  
  1561.  
  1562.  
  1563.  
  1564.  
  1565.  
  1566.  
  1567.  
  1568.  
  1569.  
  1570.  
  1571.  
  1572.  
  1573.  
  1574.  
  1575.  
  1576.  
  1577.  
  1578.  
  1579.                                        23
  1580.  
  1581.  
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588.         Output predicates
  1589.  
  1590.            display
  1591.            write
  1592.            print
  1593.            put
  1594.  
  1595.              These  functions  have  been  modified  to  accept  multiple 
  1596.         arguments in the form:
  1597.  
  1598.                        print( <arg1>, <arg2>, <arg3>,... )
  1599.  
  1600.         Thus, "put( a, b, c )" would result in the display of "abc".
  1601.              The  names  of  some  PROLOG atoms that may  occur  are  not 
  1602.         accepted  by  the  PROLOG scanner  unless  surrounded  by  single 
  1603.         quotes.  This only applies when such an atom is read in, not when 
  1604.         it is internally generated. Nevertheless, this presents us with a 
  1605.         problem:  We  would  like to be capable of writing  valid  PROLOG 
  1606.         terms to a file. In some cases, it is necessary to add the single 
  1607.         quotes.  In other cases,  such as human oriented output, they are 
  1608.         an irritant. The modified definitions of the following predicates 
  1609.         are an attempt at a solution:
  1610.  
  1611.              display
  1612.              Operator  precedence  is ignored,  all functors are  printed 
  1613.              prefix and single quotes are printed if needed or they  were 
  1614.              supplied if and when the atom was originally input.
  1615.  
  1616.              write
  1617.              Operator  precedence is taken into account and operators are 
  1618.              printed according to precedence.  Single quotes are  printed 
  1619.              under the same conditions as for "display."
  1620.  
  1621.              print
  1622.              Operator  precedence is taken into account and operators are 
  1623.              printed  according to precedence.  Single quotes  are  never 
  1624.              displayed.  This  is  a  human oriented form of  output  and 
  1625.              should  never  be used for writing of terms for  the  PROLOG 
  1626.              scanner.
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633.  
  1634.  
  1635.  
  1636.  
  1637.  
  1638.  
  1639.  
  1640.  
  1641.  
  1642.  
  1643.  
  1644.  
  1645.                                        24
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.         get0
  1655.         read
  1656.  
  1657.              The  functions  "get0"  and "read"  have  been  extended  to 
  1658.         support input from a stream other than the one currently selected 
  1659.         by "see". To direct output to a file or other stream, an optional 
  1660.         argument  is used.  For example,  "get0( char,  <file name> )" or 
  1661.         "get0( char,  user )" would cause input to come from <file  name> 
  1662.         or the console.  If the file has not already been opened,  "get0" 
  1663.         will fail.
  1664.  
  1665.  
  1666.  
  1667.              Atoms enclosed by single quotest, eg. '\nthis is a new line' 
  1668.         can contain the escape sequences
  1669.  
  1670.              '\n', '\r', '\t' and '\''.
  1671.  
  1672.              If these atoms are printed by "display" or "write" they  are 
  1673.         printed  just  as they are.  If they are printed by  the  "print" 
  1674.         clause they are translated as follows:
  1675.  
  1676.         '\n'  results  in  the printing of a carriage return and  a  line 
  1677.         feed.
  1678.         '\r' results in the printing of a carriage return only.
  1679.         '\t' results in the printing of a tab character.
  1680.         '\'' allows the printing of a single quote within a quoted atom.
  1681.  
  1682.              The "portray" feature is not presently implemented.
  1683.  
  1684.  
  1685.  
  1686.  
  1687.  
  1688.  
  1689.  
  1690.  
  1691.  
  1692.  
  1693.  
  1694.  
  1695.  
  1696.  
  1697.  
  1698.  
  1699.  
  1700.  
  1701.  
  1702.  
  1703.  
  1704.  
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711.                                        25
  1712.  
  1713.  
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720.                         Description of the New Predicates
  1721.  
  1722.  
  1723.         batch, nobatch
  1724.  
  1725.         This  predicate now makes it possible to avoid the appearance  of 
  1726.         messages and prompts on the screen when input has been redirected 
  1727.         to come from a file.
  1728.  
  1729.         The  predicate  formerly had a different effect which  no  longer 
  1730.         exists.
  1731.  
  1732.         When  placed  at  the head of a stream file which is  "seen"  the 
  1733.         system  prompt  is not repetetively displayed as  the  stream  is 
  1734.         executed. Advisory messages are not displayed either.
  1735.  
  1736.         The  command "batch" is self cancelling if control reverts to the 
  1737.         console  at any time.  It may also be cancelled by  invoking  the 
  1738.         predicate "nobatch."
  1739.  
  1740.         A related change is that when the Prolog system is invoked with a 
  1741.         command  line  argument for a stream,  the system prompt  is  not 
  1742.         initially displayed.  So if the first command in the stream  file 
  1743.         is  "batch.",  it is possible to use i/o redirection without  the 
  1744.         appearance of any system messages except aor the signon message.
  1745.  
  1746.  
  1747.  
  1748.         clearops
  1749.  
  1750.              Nullify  the  operator  status  of  every  operator  in  the 
  1751.              database.
  1752.  
  1753.         concat( (<variable> | <functor>), <List> )
  1754.  
  1755.              A  list  of functors or operators is concatenated  into  one 
  1756.         string,  which becomes the name of a new atom to which <variable> 
  1757.         or <functor> must match or be instantiated.
  1758.  
  1759.  
  1760.         dir( option )
  1761.  
  1762.              Provide  an  alphabetized listing to the console  of  atoms, 
  1763.         constants,   or   open  files.   Without  options,   simply  type 
  1764.         "dir.<CR>". Options are:
  1765.  
  1766.              dir( p ) - list clause names only.
  1767.              dir( c ) - list consulted files only.
  1768.  
  1769.         Consulted files are prefixed by "S:".
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777.                                        26
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786.         exec( arg1, arg2, ... argn )
  1787.  
  1788.              Execute  the program named as arg1,  provided that there  is 
  1789.         sufficient  memory.  arg2 through argn must be functors which are 
  1790.         supplied  as the command line argument,  separated by  one  space 
  1791.         from  neighbors.  The  PCDOS or MSDOS  operating  system  program 
  1792.         "COMMAND.COM"  must be reachable by the DOS search path,  because 
  1793.         using "exec" invokes "COMMAND.COM", which is the DOS shell.
  1794.  
  1795.              You  can temporarily leave PROLOG and execute the DOS  shell 
  1796.         with  the  invocation "exec( command )." The  DOS  prompt  should 
  1797.         appear,  and  you can run programs bearing in mind that PROLOG is 
  1798.         still  resident and occupying the bulk of memory.  To  return  to 
  1799.         PROLOG, type "exit" at the DOS prompt.
  1800.  
  1801.              This  function  is restricted in the case of the type PD  to 
  1802.         executing  a  program named "prologed",  which may be  an  editor 
  1803.         supplied by you.
  1804.  
  1805.              This  function  will not work properly unless the system  is 
  1806.         configured for memory.
  1807.  
  1808.  
  1809.         exitsys
  1810.  
  1811.              Exit to the operating system.
  1812.  
  1813.  
  1814.  
  1815.         forget( <file name> )
  1816.              Make  a database unavailable for use and reclaim the storage 
  1817.         it occupied.
  1818.  
  1819.  
  1820.  
  1821.         ratom( <arg>, <stream> )
  1822.              Read an atom from the input stream,  to which <arg>  matches 
  1823.         or  is  instantiated.  <stream> is optional.  If <stream> is  not 
  1824.         given, the input stream defaults to the standar input.
  1825.              Input is terminated by a CR or LF, which are not included in 
  1826.         the stream.
  1827.  
  1828.  
  1829.  
  1830.  
  1831.  
  1832.  
  1833.  
  1834.  
  1835.  
  1836.  
  1837.  
  1838.  
  1839.  
  1840.  
  1841.  
  1842.  
  1843.                                        27
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.                              Arithmetic Capabilities
  1853.  
  1854.              Integer  arithmetic is supported.  Numbers are 32 bit signed 
  1855.         quantities. The following arithmetic operators are supported:
  1856.  
  1857.                       "+", "-", "*", "/", <, <=, >, >=, mod.
  1858.  
  1859.         Arithmetic operators must never be used as goals,  although  they 
  1860.         may be part of structures. It is legal to write:
  1861.  
  1862.                                     X = a + b
  1863.              
  1864.         which  results in the instantiation of X to the struture (a + b). 
  1865.         But the following is not legal:
  1866.  
  1867.                        alpha( X, Y ) :- X + Y, beta( Y ).
  1868.  
  1869.  
  1870.         Evaluation  of  an arithemtic expression is mediated by the  "is" 
  1871.         and inequality predicates.  For instance,  the following would be 
  1872.         correct:
  1873.  
  1874.                          alpha( X, Y, Z ) :- Z is X + Y.
  1875.  
  1876.                          beta( X, Y ) :- X + 2 < Y + 3.
  1877.  
  1878.  
  1879.  
  1880.  
  1881.  
  1882.  
  1883.  
  1884.  
  1885.  
  1886.  
  1887.  
  1888.  
  1889.  
  1890.  
  1891.  
  1892.  
  1893.  
  1894.  
  1895.  
  1896.  
  1897.  
  1898.  
  1899.  
  1900.  
  1901.  
  1902.  
  1903.  
  1904.  
  1905.  
  1906.  
  1907.  
  1908.  
  1909.                                        28
  1910.  
  1911.  
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918.                             Memory Metric Predicate
  1919.  
  1920.              The purpose of this predicate is to give the prolog system 
  1921.         a  sense  of  how much memory remains so  that  expensive  search 
  1922.         strategies  can  be  controlled.  It is not possible  to  exactly 
  1923.         quantify how much memory remains.  At the lowest level, there are 
  1924.         two  types of memory - the stack and the heap.  The stack expands 
  1925.         down  from  high  memory,  while  the heap  tends  to  expand  at 
  1926.         unpredictable intervals upwards.  If the stack and heap meet, the 
  1927.         prolog  system  must abort the search and return to  the  prompt. 
  1928.         Judicious  use  of  the  memory  metric  predicates  reduces  the 
  1929.         probability of this happening.
  1930.  
  1931.              The  stack is easy to quantify because it expands  downwards 
  1932.         in  a  predictable  way with recursion.  The symbol  space  is  a 
  1933.         "heap".  For  those  interested,  the structure of  the  heap  is 
  1934.         determined  by  the C compiler under which Prolog  was  compiled. 
  1935.         There  is  a function internal to Prolog known as  the  allocator 
  1936.         searches  the  heap for enough contiguous memory to create a  new 
  1937.         symbol.  The  heap resembles a piece of Swiss cheese;  the  holes 
  1938.         represent symbols and already allocated memory while the remained 
  1939.         is  searched  by the allocator for a piece of  contiguous  memory 
  1940.         large enough to satisfy a request.  If one cannot be  found,  the 
  1941.         uppermost  bound of the heap is expanded upwards,  and that bound 
  1942.         is  the  number  which we measure for an  estimate  of  remaining 
  1943.         memory.
  1944.  
  1945.              The  sqace between the top of the heap,  and  the top of the 
  1946.         stack,  which we call "gap",  serves as a rough guide to how much 
  1947.         memory  remains.  The  demands  of the system  are  not  entirely 
  1948.         predictable,  however.  For example, the creation of a new symbol 
  1949.         larger  than "gap" would cause an abort.  The user must  use  the 
  1950.         numbers  supplied  by  these  functions  as  a  heuristic  guide, 
  1951.         tempered  by  experience,  to  minimize  the  possibility  of  an 
  1952.         unexpected abort.
  1953.  
  1954.         "Gap" is measured in 256 byte pages.
  1955.  
  1956.         memleft( X )
  1957.  
  1958.         If  the argument is an integer,  this is satisfied if the size of 
  1959.         "gap" is greater than "X".
  1960.  
  1961.         If the argument is a variable,  it is instantiated to the  amount 
  1962.         of "gap" remaining.
  1963.  
  1964.  
  1965.  
  1966.  
  1967.  
  1968.  
  1969.  
  1970.  
  1971.  
  1972.  
  1973.  
  1974.  
  1975.                                        29
  1976.  
  1977.  
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984.                          IBM PC Video Display Predicates
  1985.  
  1986.  
  1987.         A high level method is provided for drawing and displaying on the 
  1988.         screen of IBM PC and compatible computers. 
  1989.  
  1990.  
  1991.  
  1992.         cls
  1993.  
  1994.         Clear  the screen and position the cursor at the upper left  hand 
  1995.         corner.
  1996.  
  1997.  
  1998.  
  1999.         crtgmode( X )
  2000.  
  2001.         Matches  the  argument to the mode byte of the display  which  is 
  2002.         defined as follows:
  2003.  
  2004.                   mode           meaning
  2005.  
  2006.                    0             40 x  25  BW (default)
  2007.                    1             40 x  25  COLOR
  2008.                    2             80 x  25  BW
  2009.                    3             80 x  25  COLOR
  2010.                    4            320 x 200  COLOR
  2011.                    5            320 x 200  BW
  2012.                    6            640 x 200  BW
  2013.                    7             80 x  25  monochrome  display card
  2014.  
  2015.  
  2016.  
  2017.         crtset( X )
  2018.          
  2019.         This  sets the mode of the display.  The argument must be one  of 
  2020.         the modes given above. 
  2021.  
  2022.  
  2023.  
  2024.         curset <row>, <column>, <page> )
  2025.  
  2026.         Sets the cursor to the given row, column, and page. The arguments 
  2027.         must be integers.
  2028.  
  2029.  
  2030.  
  2031.         curwh  <row>, <column> )
  2032.  
  2033.         Reports the current position of the cursor.  The argument must be 
  2034.         an integer or variable. The format is:
  2035.  
  2036.              1) page zero is assumed.
  2037.              2) The row is in the range 0 to 79, left to right.
  2038.              3) The column is in the range 0 to 24, bottom to top.
  2039.  
  2040.  
  2041.                                        30
  2042.  
  2043.  
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050.         dotcolor( <row>, <column>, <color> )
  2051.  
  2052.         The  argument  <color> is matched to the color of  the  specified 
  2053.         dot. The monitor must be in graphics mode.
  2054.  
  2055.  
  2056.  
  2057.         drawchar( <character>, <attribute> )
  2058.  
  2059.         Put a character at the current cursor position with the specified 
  2060.         attribute.  The  arguments  <character> and <attribute>  must  be 
  2061.         integers.  Consult  the IBM technical reference manual  regarding 
  2062.         attributes.
  2063.  
  2064.  
  2065.  
  2066.         drawdot( <row>, <column>, <color> )
  2067.  
  2068.         Put  a dot at the specified position.  The monitor must be in the 
  2069.         graphics  mode.  The  arguments  must be  integer.  The  argument 
  2070.         <color> is mapped to integers by default in the following manner:
  2071.  
  2072.  
  2073.  
  2074.         drawline( <X1>, <Y1>, <X2>, <Y2>, <color> )
  2075.  
  2076.         Draw a line on the between the coordinate pairs. The monitor must 
  2077.         be in the graphics mode and the arguments are integer.
  2078.  
  2079.  
  2080.  
  2081.         prtscr
  2082.  
  2083.         Print  the  screen  as it currently appears.  Be  sure  that  the 
  2084.         printer  is  on  line and ready before invoking  this  predicate, 
  2085.         since otherwise, the system may lock up or abort.
  2086.  
  2087.  
  2088.  
  2089.         The  integer argument <color> referred to in the above predicates 
  2090.         is represented as follows:
  2091.  
  2092.              COLOR          PALETTE 0           PALETTE 1
  2093.              
  2094.                0            background          background
  2095.                1            green               cyan
  2096.                2            red                 magenta
  2097.                3            brown               white
  2098.  
  2099.         To change the palette and the background,  see the IBM  Technical 
  2100.         Reference Bios listings for more information.
  2101.  
  2102.  
  2103.  
  2104.  
  2105.  
  2106.  
  2107.                                        31
  2108.  
  2109.  
  2110.  
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116.                                The Interrupt Menu
  2117.  
  2118.                                  (type ED only)
  2119.  
  2120.              This  menu  has been modified.  It was formerly  called  the 
  2121.         ESCAPE   menu,  but  the  meaning  of the  ESCAPE  key  has  been 
  2122.         redefined.  It  is  no  longer necessary to display the  menu  to 
  2123.         perform  one of the menu functions.  This reduces the  amount  of 
  2124.         display which is lost by scrolling off the screen.
  2125.  
  2126.              Version  1.9 offers the ability to attempt a goal  from  the 
  2127.         interrupt menu, while not disturbing the ongoing computation.
  2128.  
  2129.              At any time while searching, printing, or accepting keyboard 
  2130.         input,  you can break to this menu.  It is generally not possible 
  2131.         to  do  this  during disk access,  since control  passes  to  the 
  2132.         operating  system  at this time.  Two keys cause  this  break  to 
  2133.         occur:
  2134.  
  2135.              ^V:  The menu is displayed and a command is accepted at the 
  2136.                   prompt  "INTERRUPT>".  After  a command,  the  menu  is 
  2137.                   redisplayed  until  the user selects  a  command  which 
  2138.                   causes an exit. 
  2139.  
  2140.              ^I:  The menu is not displayed.  Command is accepted at the 
  2141.                   prompt  "INTERRUPT>" until the user selects  a  command 
  2142.                   which causes an exit.
  2143.  
  2144.              ESC: Typing this key  causes a  termination of  the  PROLOG      
  2145.                   search  and  control returns to the user command  level      
  2146.                   with a prompt of "?-".  Notice that previously, the ESC      
  2147.                   key invoked this menu. 
  2148.  
  2149.  
  2150.  
  2151.  
  2152.  
  2153.  
  2154.  
  2155.  
  2156.  
  2157.  
  2158.  
  2159.  
  2160.  
  2161.  
  2162.  
  2163.  
  2164.  
  2165.  
  2166.  
  2167.  
  2168.  
  2169.  
  2170.  
  2171.  
  2172.  
  2173.                                        32
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.              As the resulting menu indicates, the following functions are 
  2183.         possible:
  2184.  
  2185.              A: Abort the search and return to the prompt.
  2186.  
  2187.              O  Open  a trace file.  The user is prompted for  the  file 
  2188.                 name.  The file receives all trace output.  If a file is 
  2189.                 already opened it is closed with all output preserved.
  2190.  
  2191.              C  Close the trace file if one is open.  Otherwise there is 
  2192.                 no effect.
  2193.  
  2194.             ^C: Immediately  exit PROLOG without closing files.  This is 
  2195.                 not advised.
  2196.  
  2197.             ^P: Typing <Control>P toggles the printer. If the printer is 
  2198.                 on,  all  input  and output will also be  routed  to  the 
  2199.                 printer.
  2200.  
  2201.              S: If  the machine in use is an IBM PC compatible  machine, 
  2202.                 the  currently displayed screen will be printed.  If  the 
  2203.                 machine  is  not an IBM PC compatible,  do not  use  this 
  2204.                 function.
  2205.  
  2206.              T: If  trace  is in use,  most of the trace output  can  be 
  2207.                 temporarily turned off by use of this function,  which is 
  2208.                 a toggle.
  2209.  
  2210.              G: Satisfy a goal.  The system prompt will appear. Do not be         
  2211.                 confused  into  thinking that the system has reverted  to 
  2212.                 the  prompt.  After the user has entered the goal in  the 
  2213.                 usual  manner,  satisfaction  of the goal is  immediately 
  2214.                 attempted.  This need not affect the running  computation 
  2215.                 in  any way,  although it is certainly possible to do so. 
  2216.                 After  the  goal is attempted,  control  returns  to  the 
  2217.                 INTERRUPT  menu.  The  purpose is to facilitate  detailed 
  2218.                 analysis of a running system.
  2219.  
  2220.              R:  Entering  another  ESC causes a return to the  current 
  2221.                 activity  (keyboard  input or search)  with  no  residual 
  2222.                 effect from the interruption.
  2223.  
  2224.  
  2225.  
  2226.  
  2227.  
  2228.  
  2229.  
  2230.  
  2231.  
  2232.  
  2233.  
  2234.  
  2235.  
  2236.  
  2237.  
  2238.  
  2239.                                        33
  2240.  
  2241.  
  2242.  
  2243.  
  2244.  
  2245.  
  2246.  
  2247.  
  2248.                              Conserving memory space
  2249.  
  2250.  
  2251.                Success popping is controlled by the predicates "popond",
  2252.         "popoffd",  "popon",  and  "popoff".  Success popping is means of 
  2253.         reclaiming  storage which is used on backtracking to  reconstruct 
  2254.         how a particular goal was satisfied.  If it is obvious that there 
  2255.         is no alternative solution to a goal this PROLOG system is  smart 
  2256.         enough to reclaim that storage. 
  2257.  
  2258.              In  this system,  succees popping is an expensive operation. 
  2259.         Therefore,  there  is a tradeoff of memory versus  time.  On  the 
  2260.         other hand, discrete use of success popping can actually speed up 
  2261.         a program by recreating structures in a more accessible form.
  2262.  
  2263.              The  definitions of the control predicates is given in  this 
  2264.         manual  and  their use is totally  optional.  The  modulation  of 
  2265.         success popping has no effect on program logic (read solution.)
  2266.              
  2267.              The  "cut"  can  save  substantial  time  and  computational 
  2268.         overhead  as well as storage.  Although the execution of the  cut 
  2269.         costs  time,  you can design your program to use cuts in critical 
  2270.         places  to  avoid unnecessary backtracking.  Thus  the  execution 
  2271.         speed of the program can actually increase. 
  2272.              
  2273.              Anyone  who  has  read Clocksin and  Mellish  is  aware,  of 
  2274.         course, that the "cut" has a powerful logical impact which is not 
  2275.         always desirable.
  2276.  
  2277.  
  2278.         popoff
  2279.  
  2280.         See the below definition.
  2281.  
  2282.  
  2283.  
  2284.         popon
  2285.  
  2286.         The  inference  engine  does complete success popping  for  goals 
  2287.         which appear after "popon".  Consider this example: 
  2288.  
  2289.           goal :-  a, popon, b, c, popoff, d.
  2290.  
  2291.         If  no alternative solutions exist for b,  then  success  popping 
  2292.         will  reclaim storage by removing unnecessary records  describing 
  2293.         how  "b"  was  satisfied.  If the Prolog system cannot  rule  out 
  2294.         possible additional solutions,  success popping will never occur, 
  2295.         regardless of your use of "popon". 
  2296.              Since goal "d" occurs after "popoff",  success popping  will 
  2297.         never occur.
  2298.  
  2299.  
  2300.  
  2301.  
  2302.  
  2303.  
  2304.  
  2305.                                        34
  2306.  
  2307.  
  2308.  
  2309.  
  2310.  
  2311.  
  2312.  
  2313.  
  2314.         popoffd
  2315.  
  2316.         If  no  "popon" or "popoff" declarations occur in a  clause,  the 
  2317.         default  action  is  determined by  "popoffd"  and  "popond".  If 
  2318.         "popoffd" has been invoked,  the default is that success  popping 
  2319.         will not occur.
  2320.  
  2321.  
  2322.  
  2323.         popond
  2324.  
  2325.         The inverse of "popoffd". Turns on default success popping.
  2326.         printf( <stream>, <term1>,<term2>,... )
  2327.  
  2328.  
  2329.  
  2330.  
  2331.  
  2332.  
  2333.  
  2334.  
  2335.  
  2336.  
  2337.  
  2338.  
  2339.  
  2340.  
  2341.  
  2342.  
  2343.  
  2344.  
  2345.  
  2346.  
  2347.  
  2348.  
  2349.  
  2350.  
  2351.  
  2352.  
  2353.  
  2354.  
  2355.  
  2356.  
  2357.  
  2358.  
  2359.  
  2360.  
  2361.  
  2362.  
  2363.  
  2364.  
  2365.  
  2366.  
  2367.  
  2368.  
  2369.  
  2370.  
  2371.                                        35
  2372.  
  2373.  
  2374.  
  2375.  
  2376.  
  2377.  
  2378.  
  2379.  
  2380.                                  Prolog Tutorial
  2381.  
  2382.  
  2383.                                   Introduction
  2384.  
  2385.  
  2386.  
  2387.  
  2388.              Probably  you  have heard of the language PROLOG within  the 
  2389.         last year or so. You probably wondered the following things:
  2390.  
  2391.         1) What does the name stand for? Names of computer languages are 
  2392.         almost always acronyms.
  2393.  
  2394.         2) What is it good for?
  2395.  
  2396.         3) Why now?
  2397.  
  2398.         4) Can I get a copy to play with?
  2399.  
  2400.              Congratulations! You obviously know the answer to the fourth 
  2401.         question. We now respond to the other three.
  2402.              
  2403.         1)  The  name  stands for "programming in logic." This  we  shall 
  2404.         elaborate on in depth later on.
  2405.  
  2406.         2) PROLOG is good for writing question answering systems.  It  is 
  2407.         also   good   for  writing  programs  that  perform   complicated 
  2408.         strategies  that  compute the best or worst way to  accomplish  a 
  2409.         task, or avoid an undesirable result.
  2410.  
  2411.         3) PROLOG was virtually unknown in this country until researchers 
  2412.         in  Japan announced that it was to be the core language  of  that 
  2413.         country's fifth generation computer project.  This is the project 
  2414.         with  which  Japan hopes to achieve a domainant position  in  the 
  2415.         world information industry of the 1990's. 
  2416.  
  2417.              PROLOG  is  one of the most unusual computer languages  ever 
  2418.         invented.  It  cannot be compared to  FORTRAN,  PASCAL,  "C",  or 
  2419.         BASIC.  The facilities complement,  rather than replace those  of 
  2420.         conventional  languages.  Although  it  has great  potential  for 
  2421.         database  work,  it  has  nothing in  common  with  the  database 
  2422.         languages used on microcomputers.
  2423.  
  2424.              Perhaps  the  best point to make is that while  conventional 
  2425.         languages are prescriptive, PROLOG is descriptive. A statement in 
  2426.         a conventional language might read:
  2427.  
  2428.              if( car_wheels = TRUE ) then
  2429.                begin
  2430.                  (some sort of procedure)
  2431.                  X = X + 1;
  2432.                end 
  2433.  
  2434.  
  2435.  
  2436.  
  2437.                                        36
  2438.  
  2439.  
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445.  
  2446.         A statment in PROLOG could just be a statment of fact about  cars 
  2447.         and wheels. There are many relationships that hold. For instance,
  2448.  
  2449.              has( car, wheels ).
  2450.  
  2451.              has( car, quant(wheels, four) ).
  2452.  
  2453.              round( wheels ).
  2454.  
  2455.         Each  of  these statments is an independent fact  relating  cars, 
  2456.         wheels,  and  the  characteristics of wheels.  Because  they  are 
  2457.         independent, they can be put into a PROLOG program by programmers 
  2458.         working separately. The man who is a specialist on car bodies can 
  2459.         say  his thing,  the wheel specialist can have his say,  and  the 
  2460.         participants can work with relative independence. And this brings 
  2461.         to light a major advantage of PROLOG:
  2462.  
  2463.  
  2464.                              PARALLEL PROGRAMMING!!!
  2465.                             
  2466.  
  2467.         With  conventional  programming languages projects can  still  be 
  2468.         "chunked",  or  divided between programmers.  But efficiency on a 
  2469.         team  project  drops  drastically below that  of  the  individual 
  2470.         programmer  wrapped  up  in  his own trance.  As  the  number  of 
  2471.         participants    grows   the   need   for   communication    grows 
  2472.         geometrically. The time spent communicating can exceed that spent 
  2473.         programming! 
  2474.              Although   PROLOG   does   not  eliminate   the   need   for 
  2475.         task  coordination,  the problem is considerably  simplified.  It 
  2476.         also provides the ability to answer questions in a "ready to  eat 
  2477.         form."  Consider your favorite BASIC interpreter.  Based upon the 
  2478.         statements about cars and wheels previously given,  could you ask 
  2479.         it the following question?   
  2480.  
  2481.                        
  2482.               has( car, X ), round( X ).
  2483.  
  2484.              Does  a  car  have anything which  is  round?  The  question 
  2485.         instructs the PROLOG interpreter to consider all the objects that 
  2486.         it  knows are possessed by a car and find those which are  round. 
  2487.         Perhaps  you are beginning to guess that PROLOG has the abilities 
  2488.         of a smart database searcher.  It can not only find the facts but 
  2489.         selectively find them and interpret them.
  2490.  
  2491.  
  2492.  
  2493.  
  2494.  
  2495.  
  2496.  
  2497.  
  2498.  
  2499.  
  2500.  
  2501.  
  2502.  
  2503.                                        37
  2504.  
  2505.  
  2506.  
  2507.  
  2508.  
  2509.  
  2510.  
  2511.  
  2512.              Consider the problem of a fault tree, as exemplified by this 
  2513.         abbreviated one:
  2514.  
  2515.  
  2516.  
  2517.         {Car won't start}
  2518.              | 
  2519.              | 
  2520.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  2521.                 (Yes)                                       v
  2522.                  |                                  {Check battery}
  2523.                  |
  2524.         [Smell gasoline](yes) --> {Try full throttle cranking}
  2525.                  |                       (failure)
  2526.         /--------/                           |
  2527.  
  2528.                             (details omitted)
  2529.  
  2530.  
  2531.  
  2532.              The fault tree is easily programmed in BASIC. Later we shall 
  2533.         show  that  PROLOG supplies a superior replacement for the  fault 
  2534.         tree.  Though the tree is capable of diagnosing only the  problem 
  2535.         for  which  it was designed,  PROLOG dynamically  constructs  the 
  2536.         appropriate  tree from facts and rules you have provided.  PROLOG 
  2537.         is not limited to answering one specific question.  Given  enough 
  2538.         information,  it  will attempt to find all deductive solutions to 
  2539.         any problem.
  2540.  
  2541.  
  2542.  
  2543.  
  2544.  
  2545.  
  2546.  
  2547.  
  2548.  
  2549.  
  2550.  
  2551.  
  2552.  
  2553.  
  2554.  
  2555.  
  2556.  
  2557.  
  2558.  
  2559.  
  2560.  
  2561.  
  2562.  
  2563.  
  2564.  
  2565.  
  2566.  
  2567.  
  2568.  
  2569.                                        38
  2570.  
  2571.  
  2572.  
  2573.  
  2574.  
  2575.  
  2576.  
  2577.  
  2578.  
  2579.                                   PROLOG PRIMER
  2580.  
  2581.         I.                       Rules and Facts     
  2582.  
  2583.  
  2584.  
  2585.              This  is  where you should start if you know  nothing  about 
  2586.         PROLOG. Let us consider a simple statment in PROLOG, such as:
  2587.  
  2588.         1)   has( car, wheels ).
  2589.  
  2590.         This  statement  is a "fact.  The word "has" in this statment  is 
  2591.         known  either  as a functor or predicate.  It is a name  for  the 
  2592.         relationship  within the parenthesis.  It implies that a car  has 
  2593.         wheels.  But  the  order  of  the words  inside  the  bracket  is 
  2594.         arbitrary and established by you. You could just as easily say:
  2595.  
  2596.         2)   has( wheels, car ).
  2597.  
  2598.         and if you wrote this way consistently,  all would be  well.  The 
  2599.         words  has,  wheels,  and car are all PROLOG atoms.  "Wheels" and 
  2600.         "car" are constants. 
  2601.              
  2602.         A   database   of  facts  can  illustrate  the   data   retrieval 
  2603.         capabilities of PROLOG. For instance:
  2604.  
  2605.         3)   has( car, wheels ).
  2606.              has( car, frame ).
  2607.              has( car, windshield ).
  2608.              has( car, engine ).
  2609.  
  2610.         You could then ask PROLOG the question:
  2611.  
  2612.         4)   has( car, Part ).
  2613.  
  2614.         The  capital  "P" of Part means that Part is a  variable.  PROLOG 
  2615.         will make Part equal to whatever constant is required to make the 
  2616.         question match one of the facts in the database. Thus PROLOG will 
  2617.         respond:
  2618.  
  2619.              Part = wheels.
  2620.              
  2621.              More?(Y/N):
  2622.  
  2623.         If you type "y" the next answer will appear:
  2624.  
  2625.              Part = frame.
  2626.  
  2627.              More?(Y/N):
  2628.  
  2629.         If you continue, PROLOG will produce the answers Part = windshield 
  2630.         and Part = engine. Finally, you will see:
  2631.  
  2632.              More?(Y/N):y
  2633.  
  2634.  
  2635.                                        39
  2636.  
  2637.  
  2638.  
  2639.  
  2640.  
  2641.  
  2642.  
  2643.  
  2644.  
  2645.              No.
  2646.          
  2647.         indicating that PROLOG has exhausted the database.  Incidentally, 
  2648.         when  a  variable is set equal to a constant or  other  variable, 
  2649.         it is said to be instantiated to that object.
  2650.  
  2651.              Notice  that  PROLOG searches the database forwards  and  in 
  2652.         this case,  from the beginning.  The forward search path is built 
  2653.         into PROLOG and cannot be changed. An author of a program written 
  2654.         in  a  prescriptive language is quite conscious of the  order  of 
  2655.         execution  of  his program,  while in PROLOG it is  not  directly 
  2656.         under his control.
  2657.              
  2658.              The other major element is the rule which is a fact which is 
  2659.         conditionally true. In logic this is called a Horn clause: 
  2660.  
  2661.  
  2662.         5)   has( X, wheels ) :- iscar( X ).
  2663.  
  2664.         The  fact iscar( car ) and the above rule are equivalent to
  2665.  
  2666.         6)   has( car, wheels).
  2667.  
  2668.         The  symbol :- is the "rule sign." The expression on the left  of 
  2669.         :-is the "head" and on the right is the body.  The variable X has 
  2670.         scope  of the rule,  which means that it has meaning only  within 
  2671.         the rule.  For instance,  we could have two rules in the database 
  2672.         using identically named variables.
  2673.  
  2674.  
  2675.         7)   has( X,  transportation ) :- 
  2676.                            has( X,  car ), has( license, X ).
  2677.  
  2678.         8)   has( X, elephant ) :- istrainer( X ), hasjob( X ).
  2679.  
  2680.         The  variables  X in the two expressions are completely  distinct 
  2681.         and have nothing to do with each other.
  2682.  
  2683.         The comma between has( X, car ) and has( license, X ) means "and" 
  2684.         or logical conjuction.  The rule will not be true unless both the 
  2685.         clauses has(X, car) and has( license, X ) are true.
  2686.  
  2687.  
  2688.              On the other hand if there is a rule
  2689.              
  2690.         9)   has( license, X ) :- passedexam( X ).
  2691.  
  2692.         consider what PROLOG will do in response to the question:
  2693.  
  2694.         10)  has( harry, transportation ).
  2695.  
  2696.         (Notice  that  harry has not been capitalized because we  do  not 
  2697.         want  it  taken as a variable.  We could,  however,  say  'Harry' 
  2698.         enclosed in single quotes.)
  2699.  
  2700.  
  2701.                                        40
  2702.  
  2703.  
  2704.  
  2705.  
  2706.  
  2707.  
  2708.  
  2709.  
  2710.  
  2711.              It  will scan the database and use (7),  in which X will  be 
  2712.         instantiated to harry. The rule generates two new questions:
  2713.  
  2714.         11)  has( harry, car ).
  2715.  
  2716.         12)  has( license, harry ).
  2717.  
  2718.         Assuming  that  harry  has  a car,  the first clause  of  (7)  is 
  2719.         satisfied and the database is scanned for a match to (12). PROLOG 
  2720.         picks  up  rule (9) in which X is instantiated to harry  and  the 
  2721.         question is now posed:
  2722.  
  2723.         13)  passedexam( harry ).
  2724.  
  2725.              If there is a fact:
  2726.  
  2727.              passedexam( harry ).
  2728.  
  2729.         in  the database then all is well and harry  has  transportation. 
  2730.         If there is not, then PROLOG will succinctly tell you:
  2731.  
  2732.              No.
  2733.  
  2734.         But  suppose Harry has money and can hire a chauffer as any  good 
  2735.         programmer  can.  That  could be made part of the program in  the 
  2736.         following way.
  2737.  
  2738.              The rule which PROLOG tried to use was:
  2739.  
  2740.         14)  has( X,  transportation ) :- 
  2741.                            has( X,  car ), has( license, X ).
  2742.  
  2743.         At any point following it there could be included another rule:
  2744.  
  2745.         15)  has( X, transportation ) :- has( X, money ).
  2746.  
  2747.         or simply the bald fact:
  2748.  
  2749.         16)  has( harry, transportation ).
  2750.  
  2751.              These  additional  rules  or  facts would  be  used  in  two 
  2752.         circumstances.  If at any point a rule does not yield a solution, 
  2753.         PROLOG   will  scan  forward  from  that  rule  to  find  another 
  2754.         applicable  one.  This process is known as "backtracking  search" 
  2755.         and can be quite time consuming.
  2756.  
  2757.  
  2758.         If  in response to the "More?" prompt you answer "y" PROLOG  will 
  2759.         search  for an additional distinct solution.  It will attempt  to 
  2760.         find an alternate rule or fact for the last rule or fact used. If 
  2761.         that  fails,  it  will back up to the antecedent rule and try  to 
  2762.         find  an alternate antecedent.  And it will continue to  back  up 
  2763.         until  it  arrives at the question you asked,  at which point  it 
  2764.         will say:
  2765.  
  2766.  
  2767.                                        41
  2768.  
  2769.  
  2770.  
  2771.  
  2772.  
  2773.  
  2774.  
  2775.  
  2776.  
  2777.              No.
  2778.  
  2779.         "Antecedent"  to a rule means that it gave rise to its' use.  For 
  2780.         example,  (7)  is  the antecedent of (9) in the  context  of  the 
  2781.         question (16).
  2782.  
  2783.  
  2784.  
  2785.  
  2786.         II.                          Grammar
  2787.  
  2788.              It is a boring subject, but it must be discussed. All PROLOG 
  2789.         statements are composed of valid terms, possibly a rule sign (":-
  2790.         "),  commas representing conjunction ("and"), and a period at the 
  2791.         very end.
  2792.              A term is a structure, constant, variable, or number.
  2793.          
  2794.              What is a structure? It is a kind of grouping:
  2795.  
  2796.              1) Structures consist of a functor, and a set of objects or
  2797.                 structures in parenthesis.
  2798.  
  2799.              2)  Objects are constants,  variables,  numbers,  or  lists, 
  2800.                 which we have not discussed yet.
  2801.  
  2802.              3)  A  constant or functor must be a string of  letters  and 
  2803.                 numbers, beginning with a lower case letter, unless
  2804.                 you  choose  to  enclose  it in single  quotes  (  'howdy 
  2805.                 pardner'  ),  in  which  case you are  freed  from  these 
  2806.                 restrictions.
  2807.              4) A  variable  must be a string of  letters  and  numbers 
  2808.                 beginning with a capital letter.
  2809.              
  2810.              5) A  functor  may optionally have  arguments  enclosed  in 
  2811.                 parenthesis , as in: hascar( X ) or hascar. 
  2812.  
  2813.         An example:  "has( X, transportation )." is a structure.
  2814.  
  2815.  
  2816.  
  2817.  
  2818.  
  2819.  
  2820.  
  2821.  
  2822.  
  2823.  
  2824.  
  2825.  
  2826.  
  2827.  
  2828.  
  2829.  
  2830.  
  2831.  
  2832.  
  2833.                                        42
  2834.  
  2835.  
  2836.  
  2837.  
  2838.  
  2839.  
  2840.  
  2841.  
  2842.         III.                    Input / Output      
  2843.  
  2844.              You   now   know  enough  to  write  simple  databases   and 
  2845.         interrogate   them  profitably.   But  before  we  examine   more 
  2846.         sophisticated  examples,  it  will be necessary to add input  and 
  2847.         output to the language. There are built in functions which appear 
  2848.         as rules which are satisfied once. Thus the statment:
  2849.  
  2850.              write( 'Hello world.' ).
  2851.  
  2852.         can be included on the right side of a rule:
  2853.  
  2854.  
  2855.         greetings(  X ) :- ishuman( X ),  write( 'Hello world.' ).  You 
  2856.         can  also write "write( X )" where X is some variable.  Note that 
  2857.         'Hello  world.' is not enclosed in double quotes.  Single quotes, 
  2858.         which denote a constant, are required. Double quotes would denote 
  2859.         a list, which is another thing entirely.
  2860.  
  2861.         Provided  that  a match to "ishuman" can be  found,  the  builtin 
  2862.         function  "write"  is  executed and the message  printed  to  the 
  2863.         screen.
  2864.              The  builtin  read( X ) reads a "structure" that  you  input 
  2865.         from the keyboard. More formally, we have
  2866.  
  2867.              read( <variable> or <constant> )
  2868.              write( <variable> or <constant> )
  2869.  
  2870.         If you write read( Input ),  then the variable "keyboard" will be 
  2871.         assigned to whatever is typed at the keyboard,  provided that the 
  2872.         input  is a valid PROLOG structure.  The builtin "read" will fail 
  2873.         if instead of Keyboard we wrote read( baloney ),  where "baloney" 
  2874.         is a constant,  and the user at the keyboard did not type exactly 
  2875.         "baloney." 
  2876.  
  2877.         When you input a structure in response to a "read" statement,  be 
  2878.         sure to end it with a period and an <ENTER>. 
  2879.  
  2880.              There  is  a convenient way of putting the cursor on  a  new 
  2881.         line. This is the builtin "nl". For example:
  2882.  
  2883.              showme :- write( 'line 1' ), nl, write( 'line 2' ). 
  2884.  
  2885.         would result in:
  2886.  
  2887.              line 1
  2888.              line 2
  2889.  
  2890.              There  is  also a primitive form of input/output for  single 
  2891.         characters. It will be discussed later.
  2892.  
  2893.  
  2894.  
  2895.  
  2896.  
  2897.  
  2898.  
  2899.                                        43
  2900.  
  2901.  
  2902.  
  2903.  
  2904.  
  2905.  
  2906.  
  2907.  
  2908.         IV.                   A Fault Tree Example
  2909.  
  2910.              Consider the "won't start" fault tree for an automobile:
  2911.  
  2912.         {Car won't start}
  2913.              | 
  2914.              | 
  2915.         [Engine  turns  over](No) --> [Battery voltage](no)-\
  2916.                 (Yes)                                       v
  2917.                  |                                  {Check battery}
  2918.                  |
  2919.         [Smell gasoline](yes) --> {Try full throttle cranking}
  2920.                  |                       (failure)
  2921.         /--------/                           |
  2922.         |           /------------------------/ 
  2923.         |           |                       
  2924.         |           |
  2925.         |  [Check for fuel line leaks](yes)-->{Replace fuel line}
  2926.         |          (no)
  2927.         |           |
  2928.         |           |
  2929.         |  [Check for defective carburator](yes)--\
  2930.         |          (no)                           v
  2931.         |                                {Repair carburator}
  2932.         \----\
  2933.              |
  2934.              |
  2935.         [Is spark present](no)-->[Do points open and close](no)-\
  2936.              |                             (yes)                v
  2937.         /----/                               |          {Adjust points}
  2938.         |           /------------------------/
  2939.         |           |
  2940.         |  [Pull distributor wire, observe spark](blue)--\
  2941.         |        (orange)                                v
  2942.         |           |                           {Check plug wires & cap}
  2943.         |           |
  2944.         |  [Measure voltage on coil primary](not 12V)--\
  2945.         |         (12V)                                v
  2946.         |           |              {Check wiring, ballast resistor}
  2947.         |           |
  2948.         |  [Check condenser with ohmmeter](conducts)--\
  2949.         |    (no conduction)                          v
  2950.         |           |                         {Replace condenser}
  2951.         |           |
  2952.         |  [Open and close points](voltage not 0 - 12)--\
  2953.         |   (voltage swings 0 - 12)                     v
  2954.         |           |                         {Fix primary circuit}
  2955.         |           |
  2956.         |  {Consider hidden fault, swap components]
  2957.         |
  2958.         |
  2959.         \-------{Call a tow truck!!}
  2960.  
  2961.  
  2962.  
  2963.  
  2964.  
  2965.                                        44
  2966.  
  2967.  
  2968.  
  2969.  
  2970.  
  2971.  
  2972.  
  2973.  
  2974.              A PROLOG program to  implement this is simple. Each statment 
  2975.         represents  a  decision point fragment of the  tree.  The  PROLOG 
  2976.         interpreter  dynamically  assembles  the tree as  it  attempts  a 
  2977.         solution. 
  2978.  
  2979.         'car wont start' :- write( 'Is the battery voltage low?' ), 
  2980.                             affirm, nl,
  2981.                             write( 'Check battery' ).
  2982.  
  2983.         'car wont start' :- write( 'Smell gasoline?' ), 
  2984.                             affirm, nl,
  2985.                             'fuel system'.
  2986.  
  2987.         'fuel system'    :- write( 'Try full throttle cranking' ).
  2988.  
  2989.         'fuel system'    :- write( 'Are there fuel line leaks?' ),
  2990.                             affirm, nl,
  2991.                             write( 'Replace fuel line.' ).
  2992.  
  2993.         'fuel system'    :- write( 'Check carburator' ).
  2994.  
  2995.         'car wont start' :- write( 'Is spark present?' ),
  2996.                             not( affirm ), nl,
  2997.                             'no spark'.
  2998.  
  2999.         'no spark'       :- write( 'Do points open and close?' ),
  3000.                             not( affirm ), nl,
  3001.                             write( 'Adjust or replace points.' ).
  3002.  
  3003.         'no spark'       :- write( 'Is the spark off the coil good?' ),
  3004.                             affirm,
  3005.                             write( 'Check plug wires and cap.' ).
  3006.  
  3007.         'no spark'       :- write( 'What is the voltage on the primary
  3008.                              of the coil: ' ), 
  3009.                             read( Volts ), 
  3010.                             Volts < 10,
  3011.                             nl,
  3012.                             write('Check wiring and ballast resistor.').
  3013.  
  3014.         'no spark'       :- write( 'Does the capacitor leak?' ),
  3015.                             affirm,
  3016.                             write( 'Replace the capacitor.' ).
  3017.  
  3018.         'no spark'       :- not( 'primary circuit' ).
  3019.  
  3020.         'primary circuit' 
  3021.                          :- write( 'Open the  points.  Voltage  across 
  3022.                               coil?:'), nl,
  3023.                             read( Openvolts ), Openvolts < 1, 
  3024.                             write(  'Close the points.  Voltage  across 
  3025.                               coil?:'),
  3026.                             read( Closevolts ), Closevolts > 10, nl,
  3027.                             write( 'Primary circuit is OK.' ). 
  3028.  
  3029.  
  3030.  
  3031.                                        45
  3032.  
  3033.  
  3034.  
  3035.  
  3036.  
  3037.  
  3038.  
  3039.  
  3040.         'no spark'       :- write( 'Consider a hidden fault. Swap
  3041.                               cap, rotor,points,capacitor.' ).
  3042.  
  3043.  
  3044.         'Car wont start' :- write( 'Get a tow truck!!' ).
  3045.  
  3046.  
  3047.                                  --End program--
  3048.  
  3049.  
  3050.              The  above  is  a  simple example of  an  expert  system.  A 
  3051.         sophisticated  system would tell you exactly the method by  which 
  3052.         it  has reached a conclusion.  It would communicate by a  "shell" 
  3053.         program  written  in PROLOG which would accept a wider  range  of 
  3054.         input   than  the  "valid  structure"  required  by  the   PROLOG 
  3055.         interpreter directly.
  3056.  
  3057.  
  3058.  
  3059.  
  3060.  
  3061.  
  3062.  
  3063.  
  3064.  
  3065.  
  3066.  
  3067.  
  3068.  
  3069.  
  3070.  
  3071.  
  3072.  
  3073.  
  3074.  
  3075.  
  3076.  
  3077.  
  3078.  
  3079.  
  3080.  
  3081.  
  3082.  
  3083.  
  3084.  
  3085.  
  3086.  
  3087.  
  3088.  
  3089.  
  3090.  
  3091.  
  3092.  
  3093.  
  3094.  
  3095.  
  3096.  
  3097.                                        46
  3098.  
  3099.  
  3100.  
  3101.  
  3102.  
  3103.  
  3104.  
  3105.  
  3106.         V.                            Lists
  3107.  
  3108.              Consider  a  shopping list given you by your wife.  It is  a 
  3109.         piece of paper with items written on it in an order that probably 
  3110.         symbolizes  their  importance.  At the top it  may  say  EGGS!!!, 
  3111.         followed by carrots, hamburger, and finally a flea collar for the 
  3112.         dog, if you can find one. In PROLOG such a list would be written:
  3113.  
  3114.         1)   [eggs, carrots, hamburger, fleacollar]
  3115.  
  3116.         The  order of a list is important so that eggs and carrots cannot 
  3117.         be reversed and PROLOG be uncaring.
  3118.  
  3119.         Let us put the list in a structure:
  3120.  
  3121.              shopping( [eggs, carrots, hamburger, fleacollar] ).
  3122.  
  3123.         Then  if you wished to isolate the head of the list you could ask 
  3124.         the question:
  3125.  
  3126.              shopping( [ Mostimportant | Rest ] ).
  3127.  
  3128.         and PROLOG would respond:
  3129.  
  3130.              Mostimportant   =  eggs,   
  3131.              Rest   =   [carrots,   hamburger, fleacollar].
  3132.  
  3133.         The vertical bar "|" is crucial here. It is the string extraction 
  3134.         operator,  which  performs  a  combination  of the  CDR  and  CAR 
  3135.         functions  of LISP.  When it appears in the context [X|Y] it  can 
  3136.         separate the head of the list from the rest, or tail.
  3137.  
  3138.  
  3139.              You  may have gained the impression that PROLOG is a  rather 
  3140.         static language capable of answering simple questions,  but it is 
  3141.         far  more powerful than that.  The string extraction operator  is 
  3142.         the  key.  It permits PROLOG to whittle a complex expression down 
  3143.         to the bare remainder.  If the rules you have given it permit  it 
  3144.         to  whittle  the  remainder  down to  nothing,  then  success  is 
  3145.         achieved. An example of this is the definition of "append."
  3146.  
  3147.              Let  us suppose you have not yet done yesterday's  shopping, 
  3148.         let alone today's. You pull it out of your wallet and sootch tape 
  3149.         it to the list your wife just gave you. Yesterday's list was:
  3150.  
  3151.              [tomatoes, onions, ketchup]
  3152.  
  3153.         Combined with [eggs, carrots, hamburger, fleacollar] we obtain
  3154.  
  3155.              [eggs,carrots,hamburger,fleacollar,tomatoes,onions,garlic].
  3156.  
  3157.         To  take one list and to attach it to the tail of another list is 
  3158.         to  "append"  the first to the second.  The PROLOG definition  of 
  3159.         append is:
  3160.  
  3161.  
  3162.  
  3163.                                        47
  3164.  
  3165.  
  3166.  
  3167.  
  3168.  
  3169.  
  3170.  
  3171.  
  3172.  
  3173.  
  3174.         Rule1:     append( [], L, L ).
  3175.  
  3176.         Rule2:     append( [X|List1], List2, [X|List3] ) :-
  3177.                       append( List1, List2, List3 ].
  3178.  
  3179.         The  general  scheme is this:  
  3180.  
  3181.         The definition consists of one rule and one fact.  The rule  will 
  3182.         be used over and over again until what little is left matches the 
  3183.         fact.  The [] stands for empty list,  which is like a bag without 
  3184.         anything in it. This is an example of a recursive definition.
  3185.              Suppose we ask:
  3186.  
  3187.              append( [a,b,c], [d,e,f], Whatgives ).
  3188.  
  3189.         1. Rule 2 is invoked with arguments ( [a,b,c], [d,e,f], Whatgives ).
  3190.         2. Rule 2 is invoked again with arguments:
  3191.              ( [b,c], [d,e,f], List3 ).
  3192.         3. Rule 2 is invoked again with arguments:
  3193.              ( [b], [d,e,f], List3 ).
  3194.         4.  The  arguments  are now ([],  [d,e,f],  List3 ).  Rule 1  now 
  3195.             matches. End.
  3196.  
  3197.         How does this cause a list to be constructed? The key is to watch 
  3198.         the   third  argument.   Supplied  by  the  user,   it  is  named 
  3199.         "Whatgives". The inference engine matches it to [X|List3] in rule 
  3200.         2. Now lets trace this as rule two is successivly invoked: 
  3201.  
  3202.  
  3203.                 Whatgives                                                
  3204.                    |                                                     
  3205.                    |                                                     
  3206.                    |                                                     
  3207.                    v                                                     
  3208.         Rule2:  [X|List3] (List1 = [b,c])                                
  3209.                  |  \                                                    
  3210.                  |   \                                                   
  3211.                  |    \                                                  
  3212.                  v     \                                                 
  3213.         Rule2:   a   [X'|List3'] (List1' = [c])                          
  3214.                       |    \                                             
  3215.                       |     \                                            
  3216.                       |      \                                           
  3217.                       v       \                                          
  3218.         Rule2:        b     [X''|List3''] (List1'' = [], ie., empty set.)
  3219.                               |    \                                     
  3220.                               |     \                                    
  3221.                               |      \                                   
  3222.         Rule1:                c       L  ( in Rule1 = [d,e,f] )              
  3223.                                                                          
  3224.         End.
  3225.  
  3226.  
  3227.  
  3228.  
  3229.                                        48
  3230.  
  3231.  
  3232.  
  3233.  
  3234.  
  3235.  
  3236.  
  3237.  
  3238.         L in rule 1 is [d,e,f] for the following reason: Notice that rule 
  3239.         2 never alters List2. It supplies it to whatever rule it invokes. 
  3240.         So L in rule 1 is the original List2, or [a,b,c].
  3241.  
  3242.              This example would not have worked if the order of rules one 
  3243.         and  two  were  reversed.  The  PROLOG  inference  engine  always 
  3244.         attempts to use the the first rule encountered. You could imagine 
  3245.         it as always reading your program from the top down in attempting 
  3246.         to  find an appropriate rule.  Since rule 2 would always satisfy, 
  3247.         an  unpleasant  thing  would  have happened  if  the  order  were 
  3248.         reversed. The program would loop forever.
  3249.  
  3250.                                  A Reading List
  3251.  
  3252.              I  hope  that  this tiny introduction to PROLOG  whets  your 
  3253.         appetite. But if you want to go on, there is no one book which  I 
  3254.         could recommend as a complete reference. This is a reflection  on 
  3255.         the depth of the subject. 
  3256.  
  3257.  
  3258.         If  you  don't  consider yourself very  technical,  or  you're  a 
  3259.         manager wishing to learn about Prolog, or you simply want as much 
  3260.         assistance as possible, a good book is:
  3261.  
  3262.              Introduction to Prolog
  3263.              Bharath, Ramachandran
  3264.              TAB books, 1985
  3265.  
  3266.         I  don't  recommend  it to those seriously interested,  or  as  a 
  3267.         precursor to the more advanced texts.
  3268.  
  3269.         The "standard text",  which throws piles of source code fragments 
  3270.         at you is:
  3271.  
  3272.              Programming In Prolog
  3273.              W.F. Clocksin and C.S. Mellish
  3274.              Springer - Verlag
  3275.              Berlin,Heidelberg,New York. 1981,1984
  3276.  
  3277.         It can be purchased directly from Springer Verlag in New York.
  3278.  
  3279.         The  authors  apparently  believe  in the  Montessori  method  of 
  3280.         instruction.  The importance of this book cannot  be  underrated, 
  3281.         however,  when you consider that it established a standard,  core 
  3282.         Prolog  dialect  simply by virtue of its publication.  The  first 
  3283.         several  chapters  are eminently readable. When  you  get  bogged 
  3284.         down, however, it might be time to switch. 
  3285.  
  3286.         If  you  think that what you want  is  detailed  explanation,  we 
  3287.         recommend:
  3288.  
  3289.              Prolog for Programmers
  3290.              Kluzniak, Feliks and Szpakowicz, Stanislaw
  3291.              APIC Studies in Data Processing No. 24
  3292.              Academic Press, 1985
  3293.  
  3294.  
  3295.                                        49
  3296.  
  3297.  
  3298.  
  3299.  
  3300.  
  3301.  
  3302.  
  3303.  
  3304.         This is a detailed text which rewards careful study. Most likely, 
  3305.         you'll  decide you didn't want detailed explanation afterall.  It 
  3306.         has  the  theoretical foundation for a university course  in  the 
  3307.         subject.
  3308.  
  3309.         Slightly  less  theoretical and very comprehensive is: 
  3310.  
  3311.              The  Art  of Prolog
  3312.              Sterling and Shapiro 
  3313.              MIT Press, 1986     
  3314.  
  3315.         One  book presents interesting "AI" applications such  as  game 
  3316.         playing,  strategy, and shell systems in  considerable  detail. 
  3317.  
  3318.         You may be interested in seeing "AI" applications such as  game 
  3319.         playing,  strategy, and shell programs in considerable  detail. 
  3320.         The   following  book  has  alot  of  code  that   with   minor 
  3321.         modification   can  be  made  to  work  (watch  the  range   of 
  3322.         precedences for operators, which is 0 to 255 in C & M Prolog).
  3323.  
  3324.                Prolog Programming for Artificial Intelligence
  3325.                Ivan Bratko                               
  3326.                Addison-Wesley, 1986
  3327.                
  3328.         Bratko's book can take one from the very fundamentals to rather 
  3329.         advanced  data structures. However, as an introduction, it  may 
  3330.         be too fast paced, requiring supplementation.
  3331.  
  3332.              If  one  purchased all of the books,  one would  undoubtedly 
  3333.         find  reward  in switching from one to the  other,  as  the  need 
  3334.         arose.
  3335.  
  3336.         The  state  of  the art is well characterized  by  the  following 
  3337.         texts:
  3338.  
  3339.              Logic Programming
  3340.              Edited by K.L. Clark and S.-A. Tarnlund
  3341.              Academic Press, 1982
  3342.  
  3343.              Academic papers describing shortfalls and extensions to  the 
  3344.              language. A potpourri.
  3345.  
  3346.  
  3347.              Logic Programming and Its Applications
  3348.              Edited by Michel Van Caneghem and David H. D. Warren
  3349.              Ablex, 1986
  3350.  
  3351.              Inspirational short-takes of state of the art applications.
  3352.  
  3353.  
  3354.              Implementations of Prolog
  3355.              Edited by J.A. Campbell
  3356.              Ellis Horwood, 1984
  3357.  
  3358.              Chronicles all the mistakes and successes of those trying to 
  3359.  
  3360.  
  3361.                                        50
  3362.  
  3363.  
  3364.  
  3365.  
  3366.  
  3367.  
  3368.  
  3369.  
  3370.              produce  a  nice  implementation.  Leaves  out  the   Warren 
  3371.              machine, which is a pity, however, since it is the father of 
  3372.              all other compilation techniques. 
  3373.  
  3374.         The above three texts are difficult, but ideal if you want to get 
  3375.         into research.
  3376.         A  purely  mathematical treatise, I recommend this text  for  the 
  3377.         pure mathematician:
  3378.  
  3379.              Foundations of Logic Programming
  3380.              J.W. Lloyd
  3381.              Springer - Verlag 1984
  3382.  
  3383.         It  is  a  proof-theoretic derivation of  the  automatic  theorem 
  3384.         prover used by PROLOG.
  3385.  
  3386.  
  3387.  
  3388.  
  3389.  
  3390.  
  3391.  
  3392.  
  3393.  
  3394.  
  3395.  
  3396.  
  3397.  
  3398.  
  3399.  
  3400.  
  3401.  
  3402.  
  3403.  
  3404.  
  3405.  
  3406.  
  3407.  
  3408.  
  3409.  
  3410.  
  3411.  
  3412.  
  3413.  
  3414.  
  3415.  
  3416.  
  3417.  
  3418.  
  3419.  
  3420.  
  3421.  
  3422.  
  3423.  
  3424.  
  3425.  
  3426.  
  3427.                                        51
  3428.  
  3429.  
  3430.  
  3431.  
  3432.  
  3433.